package org.xw.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xw.dto.GzhDTO;
import org.xw.pojo.GzhArticlePojo;
import org.xw.pojo.GzhPojo;
import org.xw.pojo.MyGzhPojo;
import org.xw.service.GzhService;
import org.xw.utils.ResponseUtil;

import java.util.List;

@RestController
@RequestMapping("/gzh")
public class GzhController {

    @Autowired
    private GzhService gzhService;

    /**
     * 注册
     * (传入registerDTO的phone和password，参数)
     *
     * @param gzhPojo
     * @return
     */
    @PostMapping("/register")
    public ResponseUtil register(@RequestBody GzhPojo gzhPojo) {
        return gzhService.registerOfficialAccount(gzhPojo);
    }

    /**
     * 注销公众号
     *
     * @param gzhId
     */
    @PostMapping("/deleteGzh")
    public ResponseUtil deleteGzh(@RequestParam("gzhId") Integer gzhId) {
        boolean b = gzhService.deleteOfficialAccount(gzhId);
        if (b){
            return ResponseUtil.success("删除成功");
        }else return ResponseUtil.error("删除失败");
    }

    /**
     * 传入公众号id，更新相应信息
     *
     * @param
     * @return
     */
    @PostMapping("/update")
    public ResponseUtil updateOfficialAccount(@RequestBody GzhPojo gzhPojo) {
        int i = gzhService.updateOfficialAccount(gzhPojo);
        if (i != 0){
            return ResponseUtil.success("更新成功");
        }
        return ResponseUtil.error("更新失败");
    }

    /**
     * 搜索公众号
     *
     * @param
     * @return
     */
    @GetMapping("/search")
    public ResponseUtil searchOfficialAccount(@RequestParam("gzhName") String gzhName) {
        return gzhService.searchOfficialAccount(gzhName);
    }

    /**
     * 用户管理公众号（关注/取关）
     *
     * @param gzhId
     * @return
     */
    @PostMapping("/manageGzh")
    public ResponseUtil followOfficialAccount(HttpServletRequest httpServletRequest,Integer gzhId) {
        return gzhService.followOfficialAccount(httpServletRequest, gzhId);
    }

    /**
     * 用户查看已关注公众号列表
     *
     * @return
     */
    @GetMapping("/list")
    public ResponseUtil getOfficialAccountList(@RequestParam("userId") Integer userId) {
        List<GzhDTO> myGzhPojoList = gzhService.getOfficialAccountList(userId);
        return ResponseUtil.success(myGzhPojoList);
    }

    /**
     * 查看某一个公众号详情
     *
     * @param
     * @return
     */
    @GetMapping("/gzhDetail")
    public ResponseUtil getOfficialAccountDetail(@RequestParam("gzhId") Integer gzhId) {
        return ResponseUtil.success(gzhService.getOfficialAccount(gzhId));
    }

    /**
     * 用户查看公众号文章列表
     * （判断查询对象是不是登录的公众号id，是则查询全部，否则只能查询status=1）
     *
     * @param
     * @return
     */
    @GetMapping("/articleList")
    public ResponseUtil getGzhArticleList(HttpServletRequest httpServletRequest, @RequestParam("gzhId") Integer gzhId) {
        List<GzhArticlePojo> gzhArticleList = gzhService.getGzhArticleList(httpServletRequest, gzhId);
        if (gzhArticleList!=null){
            if (!gzhArticleList.isEmpty()){
                return ResponseUtil.success(gzhArticleList);
            }else return ResponseUtil.error("暂未查到数据");
        }else return ResponseUtil.error("暂未查到数据");

    }

    /**
     * 用户查看公众号具体文章
     *
     * @param
     * @return
     */
    @GetMapping("/article")
    public ResponseUtil getOfficialAccountArticles(@RequestParam("id") Integer id) {
        System.out.println(gzhService.getOfficialAccountArticles(id));
        return ResponseUtil.success( gzhService.getOfficialAccountArticles(id));
    }


    /**
     * 公众号发布文章
     */
    @PostMapping("/article/publish")
    public ResponseUtil publishArticle(@RequestBody GzhArticlePojo gzhArticlePojo) {
        return gzhService.publishArticle(gzhArticlePojo);
    }

    /**
     * 公众号删除文章
     *
     * @param
     */
    @PostMapping("/article/delete")
    public ResponseUtil deleteArticle(@RequestBody GzhArticlePojo articlePojo) {
        boolean b = gzhService.deleteArticle(articlePojo);
        if (b){
            return ResponseUtil.success("删除成功");
        }else
        return ResponseUtil.error("删除失败!");
    }

    /**
     * 公众号编辑文章
     *
     * @param
     * @return
     */
    @PutMapping("/article/edit")
    public ResponseUtil updateArticle(@RequestBody GzhArticlePojo gzhArticlePojo) {
        return gzhService.updateArticle(gzhArticlePojo);
    }
}
